import 'package:flutter/material.dart';
import '../utils/util.dart';
import 'package:provider/provider.dart';
import 'package:package_info_plus/package_info_plus.dart';
class VersionCheckPage extends StatefulWidget {
  const VersionCheckPage({Key? key}) : super(key: key);
  @override
  _VersionCheckPageState createState() => _VersionCheckPageState();
}

class _VersionCheckPageState extends State<VersionCheckPage> {
  @override
  Widget build(BuildContext context) {
    // 版本检测按钮
    return Scaffold(
      appBar: AppBar(
        title: Text('版本检测'),
        toolbarHeight: 32,
        centerTitle:  false,
        backgroundColor: Colors.white,
        automaticallyImplyLeading: false,
        //字体颜色
        titleTextStyle: TextStyle(fontSize: 18,color: Colors.black),
      ),
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Colors.white,
              Colors.white,
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [  
              ElevatedButton(
                onPressed: _manualVersionCheck,
                child: Text('版本检测'),
              ),
              FutureBuilder<PackageInfo>(
                future: PackageInfo.fromPlatform(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.done) {
                    return Text(
                      '当前版本：${snapshot.data?.version ?? 'N/A'}',
                      style: TextStyle(
                        fontSize: 16,
                        color: Colors.black.withOpacity(0.8),
                      ),
                    );
                  } else {
                    return Text(
                      '当前版本:N/A',
                      style: TextStyle(
                        fontSize: 16,
                        color: Colors.black.withOpacity(0.8),
                      ),
                    );
                  }
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _manualVersionCheck() async {
    await CommonUtils.checkAndPromptUpdate(context, manual: true);
  }
}
